
/***
 * 形状
 */
class Shape{
    draw(visitor){
        throw new Error("draw")
    }
}

/***
 * 访问者。可多种形态
 */
class Visitor{
    drawCircle(shape){
        throw new Error("drawCircle")
    }
    drawRectangle(shape){
        throw new Error("drawRectangle")
    }
    drawSquare(shape){
        throw new Error("drawSquare")
    }
}

class DrawingVisitor extends Visitor{

    drawCircle(shape){
        console.log("画圆形半径：",shape.radius,"填充颜色：",shape.color)
    }
    drawRectangle(shape){
        console.log("画长方形宽高：",shape.x,shape.y,"填充颜色：",shape.color)
    }
    drawSquare(shape){
        console.log("画正方形边长：",shape.x,"填充颜色：",shape.color)
    }
}



/***
 * 圆形
 */
class Circle extends Shape{

    constructor(radius,color){
        super()
        this.radius = radius
        this.color = color
    }
    draw(visitor){
        if(visitor){
            visitor.drawCircle(this)
        }
    }
}

/***
 * 长方形
 */
class Rectangle extends Shape{

    constructor(x,y,color){
        super()
        this.x = x
        this.y = y
        this.color = color
    }
    draw(visitor){
        if(visitor){
            visitor.drawRectangle(this)
        }
    }
}

/***
 * 正方形
 */
 class Square extends Shape{

    constructor(x,color){
        super()
        this.x = x
        this.color = color
    }
    draw(visitor){
        if(visitor){
            visitor.drawSquare(this)
        }
    }
}


const square = new Square(10,"red")
const circle = new Circle(10,"yellow")
const rect = new Rectangle(10,100,"green")
const visitor = new DrawingVisitor()
const shapes = [square,circle,rect]
shapes.forEach(e=>{
    e.draw(visitor)
})